package com.example.wc;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;

import com.house.House;
import com.house.HouseToolUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_EXTERNAL_STORAGE = 1;

    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };
    //然后通过一个函数来申请
    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        verifyStoragePermissions(this);
        // 获取文件路径
        /*File inputFile = new File(Environment.getExternalStorageDirectory(), "get_list.txt");
        File outputFile = new File(getFilesDir(), "wc.txt");

        // 读取密文并解密
        String encryptedContent = readFile(inputFile);
        if (encryptedContent != null) {
            String decryptedContent = House.drp(encryptedContent);
            writeFile(outputFile, decryptedContent);
        } else {
            Log.e("MainActivity", "无法读取密文文件");
        }*/
        try{
            Log.i("start", "onCreate: 开始...");
            getGift();
        }catch (Exception e) {
            Log.e("错误信息:", e + "");
        }
    }
    public void getGift()  throws Exception{
        AssetManager assetManager = getAssets();
        BufferedReader br=new BufferedReader(new InputStreamReader(assetManager.open("get_wc.txt")));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            stringBuilder.append(line);
        }
        saveFile("/storage/emulated/0/Pictures/","gift_wc.json",House.drp(stringBuilder.toString()));
    }
    public void saveFile(String filePath, String fileName, String content) {
        try {
            //首先要去创建文件
            makeFilePath(filePath, fileName);

            FileWriter fw = new FileWriter(filePath + fileName);
            //SD卡中的路径
            fw.flush();
            fw.write(content);
            fw.close();
            Log.e("TAG", "写入成功");
        } catch (Exception e) {
            Log.e("TAG", "异常：" + e.getMessage());
            e.printStackTrace();
        }
    }
    public static void makeRootDirectory(String filePath) {
        File file = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.mkdir();
            }
        } catch (Exception e) {
            Log.i("error:", e + "");
        }
    }

    public File makeFilePath(String filePath, String fileName) {
        File file = null;
        makeRootDirectory(filePath);
        try {
            file = new File(filePath + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
    public static String encode(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return "";
        }
        try {
            return House.epp(HouseToolUtil.convert(map));
        } catch (Throwable th) {
            th.printStackTrace();
            return "";
        }
    }

    // 读取文件内容
    /*private String readFile(File file) {
        FileInputStream fis = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            fis = new FileInputStream(file);
            int character;
            while ((character = fis.read()) != -1) {
                stringBuilder.append((char) character);
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/

    // 写入文件内容
    /*private void writeFile(File file, String content) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(content.getBytes());
            Log.i("MainActivity", "解密结果已写入文件: " + file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/
}